<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <%@ include file="/static/admin/jsp/include.jsp"%>
    <style type="text/css">
        .textbox.textbox-focused .textbox-addon,
        .textbox.textbox-focused .textbox-text{
            background: #eee!important;
        }
    </style>
    <script type="text/javascript">
        var id = "${id}";
        var submitForm = function($dialog, $grid, $pjq) {
            if ($('form').form('validate')) {
                var obj=sy.serializeObject($('form'));
                var data = [];
                var imgsStr = "";
                for (var i = 0; i <= tuwen; i++){
                    if ($('#content'+i).length == 0) continue;
                    var content = $('#content'+i).val();
                    var img = $('#images'+i).getFileId();
                    if (!content&&!img){
                        continue;
                    }
                    var imgs = img?img:'';
                    var json = {content:content,img:imgs};
                    data.push(json);
                    if(imgsStr != ''){
                        imgsStr = imgsStr+","+img;
                    }else{
                        imgsStr=img;
                    }
                }
                if (data.length == 0){
                    $pjq.messager.e('内容或图片不能都为空');
                    return;
                }
                obj.goodsInfo = sy.jsonToString(data);
                obj.goodsImgs = imgsStr;

                //获取标签

                obj.tars=new Array($("#goodsType").combobox('getValues')).join();


                var url=sy.contextPath + '/goods/save';
                $.post(url, obj, function(result) {
                    if (result.code == 0) {
                        $grid.datagrid('reload');
                        $dialog.dialog('destroy');
                    } else {
                        $pjq.messager.e('保存失败,'+result.msg);
                    }
                }, 'json');
            }
        };

        $(function() {
            if(id != ''){
                $.post(sy.contextPath + '/goods/findById', {
                    id : id,
                    state : 1
                }, function(result) {
                    $('form').form('load', result);
                    if (id != '') {
                        parent.$.messager.progress({
                            text : '数据加载中....'
                        });
                        $.post(sy.contextPath + '/goods/findById', {
                            id : id
                        }, function(result) {


                            var data = sy.stringToJSON(result.goodsInfo);
                            for (var i = 0; i< data.length; i++){
                                add();
                                $('#content'+i).textbox('setValue',data[i].content);
                                if (data[i].img)
                                    $('#images'+i).setFileId(data[i].img,1,1,true);
                            }


                            $.post(sy.contextPath + '/goodsTagLibrary/selectByGoods', {
                                goodsid : result.id
                            }, function(info) {
                                $("#goodsType").combobox('setValues',info);

                            });


                            // $('#goodsImgs').setFileId(result.goodsImgs,true,1,true);
                            $('#goodsImg').setFileId(result.goodsImg,false,1,true);

                            if (result) {
                                $('form').form('load', result);
                            }
                            parent.$.messager.progress('close');
                        }, 'json');

                    }
                })
            }

            $('#art').combobox({
                url:sy.contextPath+"/user/artList",
                textField:'artDetail',
                valueField:'id',
                editable:true
            });

            //标签
            $("#goodsType").combobox({
                url:sy.contextPath+"/goodsType/typeList",
                textField:'typeName',
                valueField:'id',
                editable:false,
                multiple:true,
                /*onSelect:function(data){
                    if (id != '') {
                        parent.$.messager.progress({
                            text : '数据加载中....'
                        });
                        $.post(sy.contextPath + '/goods/findById', {
                            id : id
                        }, function(result) {


                            var data = sy.stringToJSON(result.goodsInfo);
                            for (var i = 0; i< data.length; i++){
                                add();
                                $('#content'+i).textbox('setValue',data[i].content);
                                if (data[i].img)
                                    $('#images'+i).setFileId(data[i].img,1,1,true);
                            }


                            $.post(sy.contextPath + '/goodsTagLibrary/selectByGoods', {
                                goodsid : result.id
                            }, function(info) {
                                $("#goodsType").combobox('setValues',info);

                            });


                           // $('#goodsImgs').setFileId(result.goodsImgs,true,1,true);
                            $('#goodsImg').setFileId(result.goodsImg,false,1,true);

                            if (result) {
                                $('form').form('load', result);
                            }
                            parent.$.messager.progress('close');
                        }, 'json');

                    }
                }*/

            });


        });

        var tuwen = 0;
        function add(){
            var htmls = '';

                htmls += '<tr id="tuwen'+tuwen+'" class="tuwen'+tuwen+'"><th style="width:100px;">'+('图片描述')+'</th>'+
                    '<td>'+
                    '<input id="content'+tuwen+'" name="content'+tuwen+'" class="easyui-textbox" data-options="validType:['+'length[0,300]'+'],multiline : true" missingMessage="请输入'+('图片描述')+'"/>'+
                    (tuwen > 0 ? '<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:\'fa-trash\',plain:true" onclick="delTuwen('+tuwen+')">删除</a>' : '')+
                    '</td>'+
                    '</tr>'+
                    '<tr class="tuwen'+tuwen+'">'+
                    '<th style="width:100px;">图片：</th>'+
                    '<td>'+
                    '<div class="easyui-linkbutton" id="images'+tuwen+'" multi="'+(true)+'" fileCountLimit="9" required="required" type="file" showImage="true" showBtn="true" showWidth="40" showHeight="40" bestSize="640*320" fileType="jpg,png" fileSize="200MB" buttonText="上传图片"></div>'+
                    '<label style="color: red;">*</label>'+
                    '<label>建议尺寸：宽度640，高度320，格式JPG,PNG</label>'+
                    '</td></tr>';

            $("#tuwen").append(htmls);
            $.parser.parse('#tuwen'+tuwen);
            sy.initFileUpload('images'+tuwen);
            tuwen++;
        }

        function delTuwen(index){
            $('.tuwen'+index).remove();
        }

        function yearFormatter(date){
            var y = date.getFullYear();
            var m = date.getMonth()+1;
            var d = date.getDate();
            return y;
        };

        function yearParser(s){
            if (!s) return new Date();
            var y = s;
            var date;
            if (!isNaN(y)){
                return new Date(y,0,1);
            } else {
                return new Date();
            }
        };

    </script>
</head>
<body>
<form id="form" method="post">
    <input name="id" type="hidden" />
    <div style="padding:15px;font-size: 12px">
        <table style="table-layout:fixed;" border="0" cellspacing="0" class="formtable">
            <tr>
                <th style="width:100px;">艺术品封面：</th>
                <td>
                    <div id="goodsImg" multi="false" fileCountLimit="1" type="file" showImage="true" bestSize="640*320" fileType="jpg,png"
                         showWidth="40" showHeight="40" fileSize="2MB" buttonText="艺术品封面" url="fileUpload"></div>
                </td>
                <th style="width:100px;">商品名称：</th>
                <td>
                    <input id="goodsName" name="goodsName" class="easyui-textbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:true" />
                </td>
            </tr>
            <%--<tr>
                <th style="width:100px;">商品组图：</th>
                <td colspan="3">
                    <div id="goodsImgs" multi="true" fileCountLimit="9" type="file" showImage="true" bestSize="640*320" fileType="jpg,png"
                         showWidth="40" showHeight="40" fileSize="2MB" buttonText="艺术品封面" url="fileUpload"></div>
                </td>
            </tr>--%>
            <tr >
                <th id='tuwenType'>图文</th>
                <td id='tuwenAdd' colspan="3">
                    <div id="addBtn"><a href="javascript:void(0);" class="easyui-linkbutton" onclick="add()">添加</a></div>
                    <table style="table-layout:fixed;" border="0" cellspacing="0" class="formtable" id='tuwen'>
                    </table>
                </td>
            </tr>
            <tr>
                <th style="width:100px;">宽度：</th>
                <td>
                    <input id="width" name="width" class="easyui-textbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:true" />
                </td>
                <th style="width:100px;">长度：</th>
                <td>
                    <input id="height" name="height" class="easyui-textbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:true" />
                </td>
            </tr>
            <tr>
                <th style="width:100px;">高度：</th>
                <td colspan="3">
                    <input id="high" name="high" class="easyui-textbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:false" />
                </td>
            </tr>
            <tr>
                <th style="width:100px;">艺术家：</th>
                <td>
                    <input id="art" name="goodsUploadUserId" class="easyui-commbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:true" />
                </td>
                <th style="width:100px;">艺术品年份：</th>
                <td>
                    <%--<input id="goodsTime" name="goodsTime" class="easyui-datebox" missingMessage="请输入" prompt="请输入" style="width:90%;"--%>
                           <%--data-options="required:true,formatter:yearFormatter,parser:yearParser" />--%>
                        <input id="goodsTime" name="goodsTime" class="easyui-numberbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                               data-options="required:false" />
                </td>
            </tr>
            <tr>
                <%--<th style="width:100px;">国家编码：</th>
                <td>
                    <input id="conNum" name="conNum" class="easyui-textbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:false" />
                </td>--%>
                <th style="width:100px;">后台热度参数：</th>
                <td colspan="3">
                    <input id="hotCode" name="hotCode" class="easyui-textbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:true" />
                </td>
            </tr>
            <tr>
                <th style="width:100px;">标签库：</th>
                <td colspan="3">
                    <input class="easyui-combobox" name="goodsType" id="goodsType"
                           data-options="required:true"/>
                </td>

            </tr>
            <tr>
                <th style="width:100px;">封面宽度：</th>
                <td>
                    <input id="imgWidth" name="imgWidth" class="easyui-textbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:true" />
                </td>
                <th style="width:100px;">封面高度：</th>
                <td>
                    <input id="imgHeight" name="imgHeight" class="easyui-textbox" missingMessage="请输入" prompt="请输入" style="width:90%;"
                           data-options="required:true" />
                </td>
            </tr>
        </table>
    </div>
</form>
</body>
</html>